/**
 * shopmobile for tpshop
 * ============================================================================
 * 版权所有 2015-2099 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ——————————————————————————————————————
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 飞龙  wangqh01292@163.com
 * Date: @date 2015年10月20日 下午7:19:26 
 * Description:购物车Fragment
 * @version V1.0
 */
package org.gangcai.mac.shop.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.classic.common.MultipleStatusView;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.activity.ShopDetailActivity;
import org.gangcai.mac.shop.adapter.SPShopcartListAdapter;
import org.gangcai.mac.shop.api.AppAPI;
import org.gangcai.mac.shop.bean.CartsBean;
import org.gangcai.mac.shop.manager.RetrofitManagers;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.rxlife.RxFragment;
import org.gangcai.mac.shop.util.MyWebChromeClient;
import org.gangcai.mac.shop.utils.SPConfirmDialog;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;


/**
 *  首页 -> 购物车
 *
 */
public class SPWebCartFragment extends RxFragment {
	private static final String ARG_PARAM1 = "param1";
	private static final String ARG_PARAM2 = "param2";
	private String TAG = "SPWebCartFragment";
	private String webString = "http://www.han80.com/mobile/flows.php?user_id=";
	private Context mContext;
	@BindView(R.id.shopcart_weblayout)
	WebView shopcart_weblayout;
	Unbinder unbinder;
	public SPWebCartFragment() {
	}

	public static SPWebCartFragment newInstance(String param1, String param2) {
		SPWebCartFragment fragment = new SPWebCartFragment();
		Bundle args = new Bundle();
		args.putString(ARG_PARAM1, param1);
		args.putString(ARG_PARAM2, param2);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.mContext = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.shopcart_webview, container,false);
		unbinder = ButterKnife.bind( this, view );
        String uidString=SPUtils.getInstance().getString("uid", "");
		webString="http://www.han80.com/mobile/flows.php?user_id="+uidString;
		shopcart_weblayout.loadUrl(webString);
		shopcart_weblayout.setWebViewClient(mWebViewClient);
		shopcart_weblayout.setWebChromeClient(new MyWebChromeClient() );
		WebSettings webSettings = shopcart_weblayout.getSettings();

//如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
		webSettings.setJavaScriptEnabled( true );

//支持插件
//        webSettings.setPluginsEnabled(true);

//设置自适应屏幕，两者合用
		webSettings.setUseWideViewPort( true ); //将图片调整到适合webview的大小
		webSettings.setLoadWithOverviewMode( true ); // 缩放至屏幕的大小

//缩放操作
		webSettings.setSupportZoom( false ); //支持缩放，默认为true。是下面那个的前提。
		webSettings.setBuiltInZoomControls( false ); //设置内置的缩放控件。若为false，则该WebView不可缩放
		webSettings.setDisplayZoomControls( false ); //隐藏原生的缩放控件

		return view;
	}

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}


	@Override
	public void onDetach() {
		super.onDetach();
	}


	public void refreshData(){
		shopcart_weblayout.loadUrl(webString);
	}

	@Override
	public void onPause() {
		super.onPause();

	}

	@Override
	public void onResume() {
		super.onResume();
		shopcart_weblayout.reload();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		Log.i( "Info", "onResult:" + requestCode + " onResult:" + resultCode );
		super.onActivityResult( requestCode, resultCode, data );
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onStart() {
		super.onStart();
//		refreshData();
	}

	private WebViewClient mWebViewClient = new WebViewClient() {
		@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
			String url = request.getUrl().toString();
			if(url.startsWith( "http://www.han80.com/mobile/flow.php" )){
				url=url.replace( "flow.php","flows.php" );
			}
			view.loadUrl(url);
			return super.shouldOverrideUrlLoading( view, request );
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			//do you  work
			Log.i( "Info", "BaseWebActivity onPageStarted" );
		}
	};
	private WebChromeClient mWebChromeClient = new WebChromeClient() {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			//do you work
//            Log.i("Info","onProgress:"+newProgress);
		}

		@Override
		public void onReceivedTitle(WebView view, String title) {
			super.onReceivedTitle( view, title );

		}
	};


}
